#!/bin/bash
# set +x
gitrevid=`/usr/bin/git rev-parse HEAD`
build_id=$(printf "%03d" "$BUILD_NUMBER")
task_name=$JOB_BASE_NAME

appname="user_center"
npmjobname="user_center"

ftpserverurl="bmzx/wsdt/ux/${appname}"
httpprefix="http://192.168.4.86/ftp/jenkins/${ftpserverurl}"

workspaceprefix="/haifeng/data/jenkins/workspace"
cachedirprefix="/haifeng/data/jenkins_cache_data"

CURRENTDATE=`date +"%Y%m%d%H%M%S"`
tarfilename=bmzx-wsdt-${appname}-ux-${CURRENTDATE}-${build_id}.tar

echo "git Revision: $gitrevid"
echo $gitrevid > gitrevision.txt
echo "build  task start ..."
ls -lha

#  first 
docker run --name ${task_name} --rm  -v ${workspaceprefix}/${task_name}:/opt \
 nodejs-pnpm-lan /bin/bash -c \
"pnpm install "
# second
###"pnpm install && cd packages/${appname} &&  pnpm run build-1"
docker run --name ${task_name} --rm  -v ${workspaceprefix}/${task_name}:/opt \
 nodejs-pnpm-lan /bin/bash -c \
"pnpm install  &&  cd packages/${npmjobname}  &&  pnpm run build-only "

if (( $? )); then
 echo   "ERROR! docker run  build error  "
 exit 1
fi 

# now  in workspace  dir
currentdir=`pwd`
echo "current dir is ${currentdir}"
##########################
cd packages/${npmjobname}
tar cf ${tarfilename} ./dist
if (( $? )); then
 echo   "ERROR! tar ${appname} dist dir  error  "
 exit 1
fi

echo "submit tar to ftp server ,waiting..."
lftp -u jenkins,jenkins2025 ftp://192.168.4.86:21 <<EOF
set ssl:verify-certificate no
set ftp:ssl-protect-data true
cd  ${ftpserverurl}
put ${tarfilename}
put gitrevision.txt
exit
EOF
echo "submit ${appname} tar to ftp server  successfully"
echo "http URL: ${httpprefix}/${tarfilename} "
set -x
echo "build ok"